#!/usr/bin/python
# encoding=utf-8
from SshClient import SshClient
import time
server1='10.211.55.10'
server2='10.211.55.12'
serverSSH1=SshClient(host=server1, port=22, username='chenxin', password='asdfasdf')
serverSSH2=SshClient(host=server2, port=22, username='chenxin', password='asdfasdf')

def keyFile():
    #传递keyFile文件
    serverSSH1.sexecute('openssl rand -base64 755 > ~/keyfile')
    serverSSH1.sexecute('chmod 600 ~/keyfile')
    key=serverSSH1.sexecute('cat ~/keyfile')
    serverSSH2.sexecute('rm -f ~/keyfile')
    if len(key['out'])>0:
        serverSSH2.sexecute('echo "%s">~/keyfile'%''.join(key['out']))
    serverSSH2.sexecute('chmod 600 ~/keyfile')
def closeCurrent():
    lastPid=serverSSH1.execute('pgrep mongod')
    for i in range(0, len(lastPid['out'])):
        serverSSH1.execute(('kill -s 9 %s'%lastPid['out'][i]),True)
    lastPid=serverSSH2.execute('pgrep mongod')
    for i in range(0, len(lastPid['out'])):
        serverSSH2.execute(('kill -s 9 %s'%lastPid['out'][i]),True)
def init():
    cmds=[
        'apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6',
        'echo "deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list',
        'apt-get update',
        'apt-get install -y mongodb-org']
    for i in range(0, len(cmds)):
        serverSSH1.execute(cmds[i],True)

if __name__ == '__main__':
    #init()
    closeCurrent()
    serverSSH1.execute('rm -rf /var/lib/mongodb',True)
    serverSSH1.execute('rm -rf /var/log/mongodb',True)
    serverSSH2.execute('rm -rf /var/lib/mongodb',True)
    serverSSH2.execute('rm -rf /var/log/mongodb',True)

    r=serverSSH1.execute('mkdir /var/lib/mongodb',True)
    r=serverSSH1.execute('mkdir /var/log/mongodb',True)
    serverSSH2.execute('mkdir /var/lib/mongodb',True)
    serverSSH2.execute('mkdir /var/log/mongodb',True)

    serverSSH1.execute(
        'mongod --port 27017 --bind_ip 0.0.0.0 --logpath /var/log/mongodb/mongodb.log --logappend --fork --dbpath /var/lib/mongodb --storageEngine wiredTiger  --replSet fang',
        True
        )
    
    result=serverSSH1.sexecute('mongo admin  --port 27017 --eval "rs.initiate();"')
    result=serverSSH2.execute(
        'mongod --port 27017 --bind_ip 0.0.0.0 --logpath /var/log/mongodb/mongodb.log --logappend --fork --dbpath /var/lib/mongodb --storageEngine wiredTiger  --replSet fang',
        True
        )
    result=serverSSH1.sexecute('mongo admin  --port 27017 --eval "rs.add(\\"%s:%s\\");"'%(server2,27017))
    #result=serverSSH1.sexecute('mongo admin --eval "var cfg = rs.conf();cfg.members[0].priority = 999;rs.reconfig(cfg,{force:true})"')
    result=serverSSH1.sexecute('mongo admin --port 27017 --eval "db.createUser({user: \\"gysl\\",pwd: \\"gysl\\",roles: [ \\"root\\", \\"userAdminAnyDatabase\\", \\"userAdmin\\", \\"dbOwner\\" ]});"')

    
    closeCurrent()
    keyFile()

    serverSSH1.execute(
        'mongod --port 27017 --bind_ip 0.0.0.0 --logpath /var/log/mongodb/mongodb.log --logappend --fork --dbpath /var/lib/mongodb --storageEngine wiredTiger  --replSet fang --auth  --keyFile ~/keyfile',
        True
        )

    serverSSH2.execute(
        'mongod --port 27017 --bind_ip 0.0.0.0 --logpath /var/log/mongodb/mongodb.log --logappend --fork --dbpath /var/lib/mongodb --storageEngine wiredTiger  --replSet fang --auth  --keyFile ~/keyfile',
        True
        )


